package com.yanqu.road.entity.activity.jiuzhou.guess;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
//减少setUpdate的次数
@FieldTypeAnnotation(tableName = "t_c_jiu_zhou_round_guess_data",desc = "跨服九州轮次竞猜数据")
public class CrossJiuZhouGuessData extends DataObject {
    @FieldTypeAnnotation(desc = "活动id",select = true,update = true)
    private int activityId;
    @FieldTypeAnnotation(desc = "轮次id",select = true,update = true)
    private int roundId;
    @FieldTypeAnnotation(desc = "各个赛区竞猜的房间id")
    private Map<Integer,Integer> guessRoomIdMap;                  //各个赛区竞猜的房间id
    @FieldTypeAnnotation(desc = "所有房间内用户的被竞猜次数")
    private Map<Integer,Map<Long,Integer>> allUserGuessNum;      //所有房间内用户的被竞猜次数
    @FieldTypeAnnotation(desc = "0,1,2,3 类型的竞猜结果,进入展示期后定时器更新,结束后这里很少更新")
    private Map<Integer, List<Long>> guessResultMap;
    @FieldTypeAnnotation(desc = "房间的胜利者,赛区id,胜利者id 进入展示期后定时器更新")
    private Map<Integer,Long> roomWinnerMap;

    public CrossJiuZhouGuessData(int activityId, int roundId) {
        this.activityId = activityId;
        this.roundId = roundId;
        this.guessResultMap = new ConcurrentHashMap<>();
        this.allUserGuessNum = new ConcurrentHashMap<>();
        this.guessRoomIdMap = new ConcurrentHashMap<>();
        this.roomWinnerMap = new ConcurrentHashMap<>();
    }

    public int getActivityId() {
        return activityId;
    }
    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public int getRoundId() {
        return roundId;
    }
    public void setRoundId(int roundId) {
        if (this.roundId != roundId) {
            this.roundId = roundId;
            setOp(DataStatus.Update);
        }
    }

    public java.util.Map<java.lang.Integer, java.lang.Integer> getGuessRoomIdMap() {
        return guessRoomIdMap;
    }

    public void setGuessRoomIdMap(java.util.Map<java.lang.Integer, java.lang.Integer> guessRoomIdMap) {
        if (null == guessRoomIdMap) {
            return;
        }
        this.guessRoomIdMap = guessRoomIdMap;
        setOp(DataStatus.Update);
    }

    public java.util.Map<java.lang.Integer, java.util.Map<java.lang.Long, java.lang.Integer>> getAllUserGuessNum() {
        return allUserGuessNum;
    }

    public void setAllUserGuessNum(java.util.Map<java.lang.Integer, java.util.Map<java.lang.Long, java.lang.Integer>> allUserGuessNum) {
        if (null == allUserGuessNum) {
            return;
        }
        this.allUserGuessNum = allUserGuessNum;
        setOp(DataStatus.Update);
    }

    public java.util.Map<java.lang.Integer, java.util.List<java.lang.Long>> getGuessResultMap() {
        return guessResultMap;
    }
    public void setGuessResultMap(java.util.Map<java.lang.Integer, java.util.List<java.lang.Long>> guessResultMap) {
        if (null == guessResultMap){
            return;
        }
        if (this.guessResultMap == null || !this.guessResultMap.equals(guessResultMap)) {
            this.guessResultMap = guessResultMap;
            setOp(DataStatus.Update);
        }
    }

    public java.util.Map<java.lang.Integer, java.lang.Long> getRoomWinnerMap() {
        return roomWinnerMap;
    }
    public void setRoomWinnerMap(java.util.Map<java.lang.Integer, java.lang.Long> roomWinnerMap) {
        if (null == roomWinnerMap){
            return;
        }
        this.roomWinnerMap = roomWinnerMap;
        setOp(DataStatus.Update);
    }

    /**
     * 竞猜加人数到所有人里
     * @param guessData
     */
    public void guess(Map<Integer,Long> guessData){
        if (guessData == null){
            return;
        }
        for (Map.Entry<Integer,Long> entry : guessData.entrySet()){
            int zoneId = entry.getKey();
            long userId = entry.getValue();
            if (allUserGuessNum.containsKey(zoneId)){
                if (allUserGuessNum.get(zoneId).containsKey(userId)){
                    allUserGuessNum.get(zoneId).put(userId,allUserGuessNum.get(zoneId).get(userId)+1);
                }
            }
        }
        setUpdateOption();
    }

    /**
     * 玩家更换竞猜的选择 把原有的支持人数先扣掉
     */
    public void cancelGuess(Map<Integer,Long> oldGuessMap){
        if (oldGuessMap == null){
            return;
        }
        if (oldGuessMap.isEmpty()){
            return;
        }
        //遍历旧的竞猜结果
        for (Map.Entry<Integer, Long> entry : oldGuessMap.entrySet()) {
            //分区 用户
            int zoneId = entry.getKey();
            long userId = entry.getValue();
            if (allUserGuessNum.containsKey(zoneId)) {
                if (allUserGuessNum.get(zoneId).containsKey(userId)) {
                    int oldNum = allUserGuessNum.get(zoneId).get(userId);
                    if (oldNum >= 1) {
                        allUserGuessNum.get(zoneId).put(userId, oldNum - 1);
                    }
                }
            }
        }

    }




    /**
     * 淘汰赛竞猜的房间结束上传竞猜结果
     */
    public void updateGuessResult(int zoneId,int roomId,Long roomWinner){
        //竞猜的赛区id不存在
        if (!guessRoomIdMap.containsKey(zoneId)){
            return;
        }
        //竞猜的房间id不一致
        if (guessRoomIdMap.get(zoneId) != roomId){
            return;
        }
        roomWinnerMap.put(zoneId,roomWinner);
        setUpdateOption();
    }


}
